﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using WeifenLuo.WinFormsUI.Docking;
using CustomUIControls;


namespace SalesVMT
{
    public partial class frmItemList : DockContent
    {
        int pageSize = UIParamater.PageSize;
        int currPage = 0;
        int itemCount = 0;
        int totalPage = 0;
        int _Item_ID;

        public int Item_ID
        {
            get { return _Item_ID; }
            set { _Item_ID = value; }
        }
        string _Item_Code;

        public string Item_Code
        {
            get { return _Item_Code; }
            set { _Item_Code = value; }
        }
        string _Item_Name;

        public string Item_Name
        {
            get { return _Item_Name; }
            set { _Item_Name = value; }
        }

        string _Unit_Name;

        public string Unit_Name
        {
            get { return _Unit_Name; }
            set { _Unit_Name = value; }
        }
        decimal _Item_PurchasePrice;

        public decimal Item_PurchasePrice
        {
            get { return _Item_PurchasePrice; }
            set { _Item_PurchasePrice = value; }
        }
        decimal _Item_WholesalePrice;

        public decimal Item_WholesalePrice
        {
            get { return _Item_WholesalePrice; }
            set { _Item_WholesalePrice = value; }
        }
        decimal _Item_RetailPrice;

        public decimal Item_RetailPrice
        {
            get { return _Item_RetailPrice; }
            set { _Item_RetailPrice = value; }
        }
        bool selected;

        public frmItemList( bool _selected)
        {
            InitializeComponent();
            selected = _selected;
            DisplayFrame();
            LoadData();
        }

        private void DisplayFrame()
        {
            UISetting.AddColumn(ref dgvList1, "RowNumber", "STT", "C", 50, true, true);
            UISetting.AddColumn(ref dgvList1, "Item_ID", "Khóa", "", 1, false, true);
            UISetting.AddColumn(ref dgvList1, "Item_Code", "Mã số MH", "", 130, true, true);
            UISetting.AddColumn(ref dgvList1, "Item_Name", "Tên MH", "", 200, true, true);
            UISetting.AddColumn(ref dgvList1, "CalculationUnit_Name", "ĐVT", "C", 100, true, true);
            UISetting.AddColumn(ref dgvList1, "Item_PurchasePrice", "Giá mua", "R", 120, false, true);
            UISetting.AddColumn(ref dgvList1, "Item_WholesalePrice", "Giá BB", "R", 120, true, true);
            UISetting.AddColumn(ref dgvList1, "Item_RetailPrice", "Giá BL", "R", 120, true, true);
            UISetting.AddColumn(ref dgvList1, "Origin_Name", "Xuất xứ", "C", 140, true, true);
            
            dgvList1.Columns["Item_Code"].Frozen = true;
        }
        private void LoadData()
        {
            string keyWord = "";
            if (txtKeyWord.Text.Trim().Contains("'"))
                keyWord = txtKeyWord.Text.Trim().Replace("'", "''");
            else
                keyWord = txtKeyWord.Text.Trim();

            DataTable dt = ItemDB.GetList(out itemCount, keyWord, currPage, pageSize).Tables[0];
            PageIndex();
            dgvList1.Rows.Clear();
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                dgvList1.Rows.Insert(i, 1);
                dgvList1.Rows[i].Cells["RowNumber"].Value = dt.Rows[i]["RowNumber"].ToString();
                dgvList1.Rows[i].Cells["Item_ID"].Value = dt.Rows[i]["Item_ID"].ToString();
                dgvList1.Rows[i].Cells["Item_Code"].Value = dt.Rows[i]["Item_Code"].ToString();
                dgvList1.Rows[i].Cells["Item_Name"].Value = dt.Rows[i]["Item_Name"].ToString();
                dgvList1.Rows[i].Cells["CalculationUnit_Name"].Value = dt.Rows[i]["CalculationUnit_Name"].ToString();
                dgvList1.Rows[i].Cells["Item_PurchasePrice"].Value = Convert.ToDecimal(dt.Rows[i]["Item_PurchasePrice"]).ToString("N1");
                dgvList1.Rows[i].Cells["Item_WholesalePrice"].Value = Convert.ToDecimal(dt.Rows[i]["Item_WholesalePrice"]).ToString("N1");
                dgvList1.Rows[i].Cells["Item_RetailPrice"].Value = Convert.ToDecimal(dt.Rows[i]["Item_RetailPrice"]).ToString("N1");
                dgvList1.Rows[i].Cells["Origin_Name"].Value = dt.Rows[i]["Origin_Name"].ToString();
                
            }
        }
        private void PageIndex()
        {
            totalPage = itemCount % pageSize > 0 ? (itemCount / pageSize) + 1 : (itemCount / pageSize);
            if (totalPage == 0)
                lblPageIndex.Text = "0" + "/" + totalPage.ToString();
            else
                lblPageIndex.Text = (currPage + 1).ToString() + "/" + totalPage.ToString();
        }
        private void btnFirst_Click(object sender, EventArgs e)
        {
            currPage = 0;
            LoadData();
        }

        private void btnPrevious_Click(object sender, EventArgs e)
        {
            if (currPage != 0)
                currPage--;
            LoadData();
        }

        private void btnNext_Click(object sender, EventArgs e)
        {
            if (totalPage > 0 && currPage != totalPage - 1)
                currPage++;
            LoadData();
        }

        private void btnLast_Click(object sender, EventArgs e)
        {
            if (totalPage != 0)
                currPage = totalPage - 1;
            LoadData();
        }

        private void btnFind_Click(object sender, EventArgs e)
        {
            currPage = 0;
            LoadData();
        }

        private void txtKeyWord_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
                btnFind_Click(null, null);
        }

        private void btnDel_Click(object sender, EventArgs e)
        {
            if (dgvList1.SelectedCells.Count > 0)
            {
                _Item_ID = Convert.ToInt32(dgvList1["Item_ID", dgvList1.CurrentCell.RowIndex].Value);
                if (ItemDB.Using(_Item_ID) > 0)
                {
                    MessageBox.Show("Mặt hàng này đang sử dụng! Không xóa được.", Message.Warning, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return;
                }
                if (MessageBox.Show(Message.Delete_Question, Message.Info, MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                {
                    if (ItemDB.Delete(_Item_ID))
                    {
                        Program.taskbarNotifier.Show(Message.Info, Message.Delete_Complete, UIParamater.TimeToShow, UIParamater.TimeToStay, UIParamater.TimeToHide);
                        LoadData();
                    }
                    else
                    {
                        MessageBox.Show(Message.Delete_Error, Message.Error, MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                }
            }
        }

        private void btnEdit_Click(object sender, EventArgs e)
        {
            if (dgvList1.SelectedCells.Count > 0)
            {
                _Item_ID = Convert.ToInt32(dgvList1["Item_ID", dgvList1.CurrentCell.RowIndex].Value);
                Form f = new frmItem(_Item_ID);
                if (f.ShowDialog(this) == DialogResult.OK)
                {
                    LoadData();
                }
            }
        }

        private void btnNew_Click(object sender, EventArgs e)
        {
            Form f = new frmItem(0);
            if (f.ShowDialog(this) == DialogResult.OK)
            {
                currPage = 0;
                LoadData();
            }
        }

        private void dgvList1_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex >= 0)
            {
                if (selected)
                {
                    _Item_ID = Convert.ToInt32(dgvList1["Item_ID", dgvList1.CurrentCell.RowIndex].Value);
                    _Item_Code = Convert.ToString(dgvList1["Item_Code", dgvList1.CurrentCell.RowIndex].Value);
                    _Item_Name = Convert.ToString(dgvList1["Item_Name", dgvList1.CurrentCell.RowIndex].Value);
                    _Unit_Name = Convert.ToString(dgvList1["CalculationUnit_Name", dgvList1.CurrentCell.RowIndex].Value);
                    _Item_PurchasePrice = Convert.ToDecimal(dgvList1["Item_PurchasePrice", dgvList1.CurrentCell.RowIndex].Value);
                    _Item_WholesalePrice =Convert.ToDecimal(dgvList1["Item_WholesalePrice", dgvList1.CurrentCell.RowIndex].Value);
                    _Item_RetailPrice = Convert.ToDecimal(dgvList1["Item_RetailPrice", dgvList1.CurrentCell.RowIndex].Value);
                    this.DialogResult = DialogResult.OK;
                }
                else
                btnEdit_Click(null, null);
            }
        }

        private void mnuPrint_Click(object sender, EventArgs e)
        {
            if (dgvList1.SelectedCells.Count > 0)
            {
                string Item_ID = dgvList1["Item_ID", dgvList1.CurrentCell.RowIndex].Value.ToString();
                frmItemReport f = new frmItemReport();
                if (f.ShowDialog() == DialogResult.OK)
                {
                    frmReport fReport = new frmReport();
                    if(f.ItemType == "SP DA CHON")
                        fReport = new frmReport(f.PriceType, new string[] { Item_ID});
                    else
                        fReport = new frmReport(f.PriceType, new string[] { "0"});
                    fReport.Show(this);
                }
            }
        }
    }
}